package edu.friday.controller;

import edu.friday.common.base.BaseController;
import edu.friday.common.constant.UserConstants;
import edu.friday.common.result.RestResult;
import edu.friday.common.result.TableDataInfo;
import edu.friday.model.SysUser;
import edu.friday.model.vo.SysUserVO;
import edu.friday.service.SysRoleService;
import edu.friday.service.SysUserService;
import edu.friday.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    @Autowired
    SysUserService userService;
    @Autowired
    SysRoleService sysRoleService;


    @GetMapping("/list")
    public TableDataInfo list(SysUserVO user, Pageable page){
        int pageNum=page.getPageNumber()-1;
        pageNum=pageNum<=0 ? 0 : pageNum;
        page= PageRequest.of(pageNum,page.getPageSize());
        return userService.selectUserList(user,page);
    }

    @PostMapping
    public RestResult add(@RequestBody SysUserVO userVO){
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(userVO.getUserName()))) {
            return RestResult.error("新增用户"+userVO.getUserName()+"失败，登陆账号已存在");
        }else if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(userVO))){
            return RestResult.error("新增用户"+userVO.getUserName()+"失败，手机号已存在");
        }else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(userVO))){
            return RestResult.error("新增用户"+userVO.getUserName()+"失败，邮箱已存在");
        }userVO.setCreateBy("system");
        boolean flag=userService.insertUser(userVO);
        return toAjax(flag ?1:0);
    }

    @DeleteMapping("/{userIds}")
    public RestResult remove(@PathVariable Long[] userIds){
        return toAjax(userService.deleteUserByIds(userIds));
    }

    @PutMapping
    public RestResult edit(@Validated @RequestBody SysUserVO userVO){
        if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(userVO))) {
            return RestResult.error("修改用户"+userVO.getUserName()+"失败，手机号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(userVO))) {
            return RestResult.error("修改用户"+userVO.getUserName()+"失败，邮箱号已存在");
        }
        userVO.setUpdateBy("system");
        boolean flag=userService.updateUser(userVO);
        return toAjax(flag ?1:0);
    }

    @GetMapping(value = {"/","/{userId}"})
    public RestResult getInfo(@PathVariable(value = "userId",required = false) Long userId){
        RestResult ajax=RestResult.success();
        ajax.put("roles",sysRoleService.selectRoleAll());
        if (StringUtils.isNotNull(userId)) {
            ajax.put(RestResult.DATA_TAG,userService.selectUserById(userId));
            ajax.put("roleIds",sysRoleService.selectRoleListByUserId(userId));
        }
        return ajax;
    }

    @PostMapping("/adds")
    public RestResult adds(@RequestBody SysUserVO userVO){
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(userVO.getUserName()))) {
            return RestResult.error("新增用户"+userVO.getUserName()+"失败，登陆账号已存在");
        }else if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(userVO))){
            return RestResult.error("新增用户"+userVO.getUserName()+"失败，手机号已存在");
        }else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(userVO))){
            return RestResult.error("新增用户"+userVO.getUserName()+"失败，邮箱已存在");
        }userVO.setCreateBy("system");
        userService.insertUsers(userVO);
        return RestResult.success();
    }




}
